/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:FloatTypeHandler.java   2020-07-18 21:43 wanglijun
 */
package cn.datacare.orm.mapping.type.handler;

import cn.datacare.orm.mapping.type.JavaSqlTypeHandler;
import cn.datacare.orm.mapping.type.PrimitiveValue;
import cn.datacare.orm.mapping.type.TypeParameter;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-07-18 21:43
 */
public class FloatTypeHandler implements JavaSqlTypeHandler, PrimitiveValue {

    /***
     * 默认值
     */
    private static final float DEFAULT_VALUE = 0.0F;

    /***
     *   基础类，用来sql到java，java到sql的映射
     * @param typeParameter
     * @return Object
     * @throws SQLException
     */
    @Override
    public Object getValue(TypeParameter typeParameter) throws SQLException {
        ResultSet rs = typeParameter.getResultSet();
        float value = rs.getFloat(typeParameter.getIndex());
        if (!rs.wasNull()) {
            return value;
        }
        if (typeParameter.isPrimitive()) {
            return getDefaultValue();
        }
        return null;
    }

    /***
     * 获取默认值
     * @return Object
     */
    @Override
    public Object getDefaultValue() {
        return DEFAULT_VALUE;
    }
}
